﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace TV4Home.Client.WMC.TVClient.UI
{
    /// <summary>
    /// Contains settings for the plugin.
    /// </summary>
    public class ApplicationSettings
    {
        #region Private Fields
        private static ApplicationSettings _instance;
        private static string _settingsFilePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\TV4Home TV Client\ApplicationSettings.xml";
        #endregion

        #region Constructor
        public ApplicationSettings()
        {
            // set default values
            this.Language = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
            this.ServerHostName = "";
            this.TVWebServiceURL = "http://{0}/TV4Home/WebService/TVWebService.asmx";
            this.IsConfigured = false;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the language to use in the application.
        /// </summary>
        /// <value>The language to use in the application.</value>
        public string Language { get; set; }
        /// <summary>
        /// Gets or sets the name of the server host.
        /// </summary>
        /// <value>The name of the server host.</value>
        public string ServerHostName { get; set; }
        /// <summary>
        /// Gets or sets the TV web service URL.
        /// </summary>
        /// <value>The TV web service URL.</value>
        public string TVWebServiceURL { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether the application is configured.
        /// </summary>
        /// <value><c>true</c> if configured; otherwise, <c>false</c>.</value>
        public bool IsConfigured { get; set; }

        /// <summary>
        /// Gets the static instance of this class.
        /// </summary>
        /// <value>The static instance of this class.</value>
        public static ApplicationSettings Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ApplicationSettings(); // create default instance

                    System.IO.DirectoryInfo settingsDir = new System.IO.DirectoryInfo(System.IO.Path.GetDirectoryName(_settingsFilePath));

                    if (!settingsDir.Exists)
                        settingsDir.Create();

                    _instance = XML.LoadConfiguration<ApplicationSettings>(_settingsFilePath, true);
                }

                return _instance;
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Saves the current settings to disk.
        /// </summary>
        public void Save()
        {
            if (_instance != null)
                XML.WriteConfiguration(_settingsFilePath, _instance);
        }
        #endregion
    }
}
